package cn.chengpengper.seckill.user.interfaces.controller;

import cn.chengpengper.seckill.common.code.ErrorCode;
import cn.chengpengper.seckill.common.model.dto.SeckillUserDTO;
import cn.chengpengper.seckill.common.response.ResponseMessage;
import cn.chengpengper.seckill.common.response.ResponseMessageBuilder;
import cn.chengpengper.seckill.user.application.service.SeckillUserService;
import cn.chengpengper.seckill.user.domain.model.entity.SeckillUser;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户接口
 *
 * @author chengpeng.hu
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*", originPatterns = "*")
public class SeckillUserController {

    private final SeckillUserService seckillUserService;

    /**
     * 用户登录
     *
     * @param seckillUserDTO 用户登录信息
     * @return token
     */
    @PostMapping("/v1/login")
    public ResponseMessage<String> login(@RequestBody SeckillUserDTO seckillUserDTO) {
        String token = seckillUserService.login(seckillUserDTO.getUserName(), seckillUserDTO.getPassword());
        return ResponseMessageBuilder.build(ErrorCode.SUCCESS.getCode(), token);
    }

    /**
     * 根据用户ID获取用户信息
     *
     * @param userId 用户ID
     * @return 用户信息
     */
    @GetMapping("/v1/getUser")
    public ResponseMessage<SeckillUser> getUser(@RequestAttribute Long userId) {
        SeckillUser user = seckillUserService.getSeckillUserByUserId(userId);
        return ResponseMessageBuilder.build(ErrorCode.SUCCESS.getCode(), user);
    }
}
